$(document).ready(function() {
	$('#contact').submit(function() {
		var abort = false;
		$("div.error").remove();
		$(':input[data-validation="required"]').each(function() {
			if ($(this).val()===this.defaultValue | $(this).val()==="") {
				$(this).after('<div class="error">This is a required field</div>');
				abort = true;
			}
		}); // go through each data-validation="required" value
		if (abort) { 
			return false; 
		} else {
		var forminfo = new Array();
		var count=0;	
		$(':input[data-validation="required"]').each(function() {
			count=count+1;
			forminfo[count]=$(this).val();
		});
		var name2, company2, city2, country2, email2, phone2, subject2, message2;
		name2=forminfo[1];
		company2=forminfo[2];
		city2=forminfo[3];
		country2=forminfo[4];
		email2=forminfo[5];
		phone2=forminfo[6];
		subject2=forminfo[7];
		message2=forminfo[8];
		
		$.post("/wp-content/themes/fructa/send_form_email.php",
			{
				name:name2,
				company:company2,
				city:city2,
				country:country2,
				email:email2,
				phone:phone2,
				subject:subject2,
				message:message2
			}, function(data){
					$('.f-modal').show();
					$('.f-mask').show();
			}

			);
		//alert(forminfo);

		return false;
		}
	})//on submit
	
	$('.btn-success').click(function() {
		$('.f-modal').hide();
		$('.f-mask').hide();
		$('#contact').each (function(){
		this.reset();
		});
	})


}); // ready

$('input[pattern]').blur(function() {
	$("div.error2").remove();
	$("div.error").remove();
	var myPattern = $(this).attr('pattern');
	var isValid = $(this).val().search(myPattern) >= 0;

	if (!isValid) {
		$(this).focus();
		$(this).after('<div class="error2">Wrong pattern, use: ' + this.defaultValue + '</div>');
	} // isValid test
}); // onblur

